{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# opencv 最小外接四边形"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "53.13010025024414\n"
     ]
    }
   ],
   "source": [
    "import cv2\n",
    "import numpy as np\n",
    "data = np.array([[40,0],[0,30],[30,70],[70,40]])\n",
    "rect = cv2.minAreaRect(data)  # 得到最小外接矩形的（中心(x,y), (宽,高), 旋转角度）\n",
    "c_x = rect[0][0]\n",
    "c_y = rect[0][1]\n",
    "w = rect[1][0]\n",
    "h = rect[1][1]\n",
    "theta = rect[-1]\n",
    "print(theta)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 标注可视化"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ok\n"
     ]
    }
   ],
   "source": [
    "from PIL import ImageDraw,Image\n",
    "image_path = r'f:/dataset\\object_detection\\dota1.5\\rotated_data\\train_data\\images\\train\\P0000_0512_1536.png'\n",
    "img = Image.open(image_path)  #打开图片1.jpg\n",
    "draw = ImageDraw.Draw(img)\n",
    "draw.line([(396, 729), (440, 626),(577,679),(530,791),(396, 729)], fill=(255, 0, 0), width=2)\n",
    "draw.line([(396, 729), (546, 876),(543,989),(545,789),(396, 729)], fill=(255, 0, 0), width=2)\n",
    "img.save('test.png')\n",
    "print('ok')\n",
    "\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 挑出小部分数据集做训练测试"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 443/443 [00:14<00:00, 30.65it/s]\n"
     ]
    }
   ],
   "source": [
    "import os,shutil\n",
    "from tqdm import tqdm\n",
    "\n",
    "images_dir = r'E:\\workspace\\rotation-yolov5\\dataset\\dota1.5\\rotated_data\\train_data\\images\\train'\n",
    "labels_dir = r'E:\\workspace\\rotation-yolov5\\dataset\\dota1.5\\rotated_data\\train_data\\labels\\train'\n",
    "images_test_dir = r'E:\\workspace\\rotation-yolov5\\dataset\\dota1.5\\rotated_data\\train_data\\test\\images'\n",
    "labels_test_dir = r'E:\\workspace\\rotation-yolov5\\dataset\\dota1.5\\rotated_data\\train_data\\test\\labels'\n",
    "list_images_name = os.listdir(images_dir)[::20]\n",
    "for file_name in tqdm(list_images_name):\n",
    "    image_path = os.path.join(images_dir,file_name)\n",
    "    label_path = os.path.join(labels_dir,file_name.strip('.png')+'.txt')\n",
    "    shutil.copy(image_path,images_test_dir)\n",
    "    shutil.copy(label_path,labels_test_dir)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 天池比赛数据集，将image和mask分开保存"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 32034/32034 [09:35<00:00, 55.64it/s] \n"
     ]
    }
   ],
   "source": [
    "import os,shutil\n",
    "from tqdm import tqdm\n",
    "raw_dir = r'F:\\dataset\\天池比赛数据\\suichang_round1_train_210120\\suichang_round1_train_210120'\n",
    "images_dir = r'F:\\dataset\\天池比赛数据\\suichang_round1_train_210120/images'\n",
    "masks_dir = r'F:\\dataset\\天池比赛数据\\suichang_round1_train_210120/masks'\n",
    "os.makedirs(images_dir,exist_ok=True)\n",
    "os.makedirs(masks_dir,exist_ok=True)\n",
    "for file in tqdm(os.listdir(raw_dir)):\n",
    "    file_path = os.path.join(raw_dir,file)\n",
    "    if file.endswith('.tif'):\n",
    "        shutil.copy(file_path,images_dir)\n",
    "    elif file.endswith('.png'):\n",
    "        shutil.copy(file_path,masks_dir)\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5.4\n"
     ]
    }
   ],
   "source": [
    "from pathlib import Path\n",
    "import cv2\n",
    "from tqdm import tqdm\n",
    "import numpy as np\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}